#!/usr/bin/php
<?php

date_default_timezone_set('Europe/Vienna');

$FILETYPES = array(
    "Audio" => "wav,aiff,mp3,ac3,omf-audio",
    "Video" => "mov,mp4,wmv,flv,m2v,mpv,m4v,avi,r3d,mx,mxf,omf-video",
    "Bild" => "jpg,bmp,png,psd,eps,ai,psd,gif",
    "Projektfile" => "prproj,fcp,aep,sesx,dspproj,3d-files,ncor"
);

$DEFAULT_FILETYPE="Sonstiges";

// OUTPUTS
$FOLDER="";
$TYPE=$DEFAULT_FILETYPE;
$TECH="";
$SIZE="";
$DATE=date('d.m.Y');

// check if used correctly
if (count($argv)!=2) {
	echo "Usage: $argv[0] filename";
	exit(1);
}

$file=$argv[1];

// check if given file exists
if (!doesFileExist($file)) {
   	echo "$file does not exist";
	exit(1);
}

// check if folder or file
if (isFile($file)) {
	$FOLDER="Datei";
	
	$SIZE=humanFilesize(filesize($file));
	
}
else {
	$FOLDER="Ordner";
}

// set type
$ext=getFileExtension($file);

// search for type
foreach ($FILETYPES as $key => $value) {
    $exts=split(',',$value);
    
    if (in_array($ext,$exts)) {
    	$TYPE=$key;
    	break;
    }
}

// technische info
if ($TYPE=="Video" || $TYPE=="Audio" || $TYPE=="Bild") {
	$data=getFileInfo($file);
	
	
	if (array_key_exists("movie duration",$data)) {
		$duration=$data["movie duration"];
		$duration=substr($duration, 0, strpos($duration, ' '));
		$TECH.="Dauer: ".humanDuration($duration)."\n";
		
		$bitrate=round(filesize($file)*8/$duration/1024);
		
		$TECH.="Bitrate: ~".$bitrate."kb\n";
	}
	
		
	if (array_key_exists("video format",$data)) {
		$video=$data["video format"];
		$TECH.="Video Format: ".$video."\n";
	}
	
	if (array_key_exists("audio format",$data)) {
		$audio=$data["audio format"];
		$TECH.="Audio Format: ".$audio."\n";
	}
		
	if (array_key_exists("sound format",$data)) {
		$audio=$data["sound format"];
		$TECH.="Audio Format: ".$audio."\n";
	}	
		
	if (array_key_exists("width/height/depth",$data)) {
		$resolution=$data["width/height/depth"];
		$resolution=split(" / ",$resolution);

		$TECH.="Auflösung: ".$resolution[0]."x".$resolution[1]." mit ".$resolution[2]." Bit\n";
	}
	
	if (array_key_exists("sampling rate",$data)) {
		$rate=$data["sampling rate"];
		$TECH.="Audio Sampling Rate: ".$rate."H\n";
	}
	
	
	$TECH=trim($TECH);
}


echo 'user_folder {'.$FOLDER.'} user_date {'.$DATE.'} user_size {'.$SIZE.'} user_type {'.$TYPE.'} user_tech {'.$TECH.'}';

/*
$FOLDER="";
$TYPE=$DEFAULT_FILETYPE;
$TECH="";
$SIZE="";
$DATE=date('d.m.Y');
*/



// FUNCTIONS
function doesFileExist($file) {
	return file_exists($file);
}

function isFile($file) {
	return !is_dir($file);
}

function getFileExtension($file) {
	return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}

function humanDuration($seconds,$decimals = 2) {
	$size = array('s','m','h');
    $factor=0;
    
    while($seconds>60) {
    	$seconds=$seconds/60;
    	$factor++;
    }
    
    if ($factor>=count($size)) {
    	$factor=count($size)-1;
    }
    
    return sprintf("%.{$decimals}f", $seconds) . @$size[$factor]; 

}

function humanFilesize($bytes, $decimals = 2) {
    $size = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
    $factor = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
} 

function getFileInfo($file) {
	$QT_INFO="/presstore-converter/converters/qt_tools/qt_tools.app/Contents/MacOS/qt_info";
	
	$data=shell_exec("$QT_INFO \"$file\"");
	$data=str_replace("+","",$data);
	$lines=split("\n",$data);
	
	$items=array();
	
	foreach ($lines as $line) {
		$item=split(" : ",$line);
		$item=array_map('trim',$item);
		
		if (count($item)==2) {
			$items[$item[0]]=$item[1];
		}
	}

	return $items;

}

?>